//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.CustomerComplaintAudit;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerComplaintAuditDao extends BaseDao<CustomerComplaintAudit, Integer> {
	public CustomerComplaintAuditDao() {
	}

	public List<CustomerComplaintAudit> getListByComplaintId(int complainId) {
		return this.queryList("complainId=:p1", new Object[]{Integer.valueOf(complainId)}, (new OrderSet()).asc("autoId"), -1);
	}

	public int getAuditStatus5(int complainId) {
		byte st = 0;
		String sql = "select top 1 auditType, groupCode AuditUser\nfrom CustomerComplaintAudit a inner join SysUser u on a.auditUser=u.userCode\nwhere ComplainId=:p1 order by auditTime desc";
		CustomerComplaintAudit audit = (CustomerComplaintAudit)this.getBySql(CustomerComplaintAudit.class, sql, new Object[]{Integer.valueOf(complainId)});
		if(audit != null && (audit.getAuditType().intValue() == 4 || audit.getAuditType().intValue() == 8)) {
			if(!"G111".equalsIgnoreCase(audit.getAuditUser()) && !"G110".equalsIgnoreCase(audit.getAuditUser())) {
				if("G121".equalsIgnoreCase(audit.getAuditUser()) || "G120".equalsIgnoreCase(audit.getAuditUser())) {
					st = 2;
				}
			} else {
				st = 1;
			}
		}

		return st;
	}

	public boolean saveAudit(CustomerComplaintAudit audit) {
		this.add(audit);
		String sql;
		if(audit.getAuditResult().intValue() == 1) {
			if(audit.getAuditType().intValue() == 4 || audit.getAuditType().intValue() == 8) {
				String bst12 = (String)this.getBySql(String.class, "select type from CustomerComplaint where complainId=:p1", new Object[]{audit.getComplainId()});
				if("5".equals(bst12)) {
					sql = "select distinct groupCode\nfrom CustomerComplaintAudit a inner join SysUser u on a.auditUser=u.userCode\nwhere ComplainId=:p1 and auditType=:p2 and groupCode in (\'G111\',\'G110\',\'G121\',\'G120\')\nand auditTime>(select max(auditTime) from CustomerComplaintAudit where ComplainId=:p3 and auditType<:p4)";
					List pt = this.queryListBySql(String.class, sql, new Object[]{audit.getComplainId(), audit.getAuditType(), audit.getComplainId(), audit.getAuditType()});
					if(pt.size() < 2) {
						return true;
					}
				}
			}

			boolean bst121 = false;
			if(audit.getAuditType().intValue() == 7 && audit.getAuditAdd().intValue() != 1 || audit.getAuditType().intValue() == 8) {
				String pt1 = (String)this.getBySql(String.class, "select procType from CustomerComplaint where complainId=:p1", new Object[]{audit.getComplainId()});
				if("6".equals(pt1)) {
					bst121 = true;
				}
			}

			if(bst121) {
				sql = "update CustomerComplaint set status=12, updateTime=:p1 where complainId=:p2";
			} else if(audit.getAuditType().intValue() == 13) {
				sql = "update CustomerComplaint set status=9, updateTime=:p1 where complainId=:p2";
			} else if(audit.getAuditType().intValue() == 7 && audit.getAuditAdd().intValue() != 1) {
				sql = "update CustomerComplaint set status=status+2, updateTime=:p1 where complainId=:p2";
			} else {
				sql = "update CustomerComplaint set status=status+1, updateTime=:p1 where complainId=:p2";
			}
		} else if(audit.getAuditResult().intValue() == 3) {
			sql = "update CustomerComplaint set status=11, updateTime=:p1 where complainId=:p2";
		} else {
			sql = "update CustomerComplaint set status=1, updateTime=:p1 where complainId=:p2";
		}

		return this.updateBySql(sql, new Object[]{new Date(), audit.getComplainId()}) > 0;
	}
}
